<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>点光源案例</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }

    </style>
</head>
<body onload="draw();">

</body>
<script src="../js/three.js"></script>
<script src="../js/stats.min.js"></script>
<script src="../js/dat.gui.min.js"></script>
<script>
    var renderer, camera, scene, gui, stats, ambientLight, pointLight;
    var debug, sphere, cube, plane;

    function initRender() {
        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);
        //告诉渲染器需要阴影效果
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是，没有设置的这个清晰 THREE.PCFShadowMap
        document.body.appendChild(renderer.domElement);
    }

    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(0, 100, 200);
        camera.lookAt(new THREE.Vector3(0, 0, 0));
    }

    function initScene() {
        scene = new THREE.Scene();
    }

    function initGui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            ambientLight: "#111111", //环境光源
            pointLight: "#ffffff", //点光源
            intensity: 1, //灯光强度
            visible: true, //是否可见
            castShadow: true, //是否开启阴影
            debug: true, //开启关闭光照辅助
            x: 40, //朝向x轴坐标
            y: 60, //朝向y轴坐标
            z: 10, //朝向z轴坐标
        };

        var datGui = new dat.GUI();
        //将设置属性添加到gui当中，gui.add(对象，属性，最小值，最大值）
        datGui.addColor(gui, "ambientLight").name("环境光颜色").onChange(function (e) {
            ambientLight.color.set(e);
        });
        datGui.addColor(gui, "pointLight").name("点光源颜色").onChange(function (e) {
            pointLight.color.set(e);
        });
        datGui.add(gui, "intensity", 0, 5).name("光线强度").onChange(function (e) {
            pointLight.intensity = e;
        });
        datGui.add(gui, "visible").name("显示光").onChange(function (e) {
            pointLight.visible = e;
        });
        datGui.add(gui, "castShadow").name("显示阴影").onChange(function (e) {
            pointLight.castShadow = e;
        });
        datGui.add(gui, "debug").name("开启光照辅助").onChange(function (e) {
            debug.visible = e;
        });

        datGui.add(gui, "x", -50, 50).name("X坐标位置").onChange(changePosition);
        datGui.add(gui, "y", 0, 200).name("Y坐标位置").onChange(changePosition);
        datGui.add(gui, "z", -50, 50).name("Z坐标位置").onChange(changePosition);

        //修改球体位置的方法，平行光的target就是球
        function changePosition() {
            pointLight.position.set(gui.x, gui.y, gui.z);
        }
    }

    function initLight() {
        ambientLight = new THREE.AmbientLight("#111111");
        scene.add(ambientLight);

        pointLight = new THREE.PointLight("#ffffff");
        pointLight.position.set(40, 60, 10);

        //告诉平行光需要开启阴影投射
        pointLight.castShadow = true;

        scene.add(pointLight);

        //添加灯光辅助
        debug = new THREE.PointLightHelper(pointLight);
        debug.name = "debug";
        scene.add(debug);
    }

    function initModel() {

        //球体
        var sphereGeometry = new THREE.SphereGeometry(5, 24, 16);
        var sphereMaterial = new THREE.MeshPhongMaterial({color: 0xff00ff});

        sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);

        sphere.castShadow = true; //开启阴影

        scene.add(sphere);

        //立方体
        var cubeGeometry = new THREE.CubeGeometry(10, 10, 10);

        var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff});

        cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.position.x = 30;
        cube.position.z = -5;

        cube.castShadow = true; //开启阴影

        scene.add(cube);

        //底部平面
        var planeGeometry = new THREE.PlaneGeometry(100, 100);
        var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa});

        plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.y = -5;

        plane.receiveShadow = true; //可以接收阴影

        scene.add(plane);

    }

    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    function render() {
        renderer.render(scene, camera);
    }

    //窗口变动触发的函数
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    function animate() {
        //更新控制器
        render();

        //更新性能插件
        stats.update();

        //更新光照辅助
        if (debug) {
            debug.update();
        }

        requestAnimationFrame(animate);
    }

    function draw() {
        initGui();
        initRender();
        initScene();
        initCamera();
        initLight();
        initModel();
        initStats();

        animate();
        window.onresize = onWindowResize;
    }
</script>
</html>